var app = getApp()
Page({
    data: {
        activeIndex: 0,
        data: '',
        numArray: ['100元', '500元', '1000元', '其他'],
        money: '',

        show: false,
        type:'',            // 充值类型  margin: 保证金充值
    },
    valmoney(e) {
        this.setData({money: e.detail.value})
    },
    //点击选中事件
    activethis: function(e) { 
        var thisindex = e.currentTarget.dataset.thisindex; //当前index
        var thistext = e.currentTarget.dataset.select;
        this.setData({
            activeIndex: thisindex,
        })
        if (thisindex == 3) {
            this.setData({
                show: true,
            })
        } else {
            this.setData({
                money: thistext,
                show: false,
            })
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var numArray = this.data.numArray;
        var charge_type = options.charge_type || '';

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/userinfo',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                money: numArray[0],
                                charge_type: charge_type,
                                type: options.type || '',
                                data: res.data.data
                            })
                        }
                    }
                })
            }
        })
    },
    charge: function(e) {
        var that = this;
        var type = this.data.type;
        var charge_type = this.data.charge_type;
        wx.showLoading({title: '跳转中'})

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/pays/charge',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        type: charge_type,
                        money: that.data.money,
                    },
                    success: function(res) {
                        var list = res.data;

                        if (list.code == 1) {
                            wx.hideLoading();
                            wx.navigateTo({
                                url: `../pay-style/pay-style?money=${list.data.money}&order_no=${list.data.trade_no}&order_type=charge_order&type=${type}`,
                            })
                        }
                    }
                })
            }
        })
    }
})